package uk.ac.aber.paws.server.core;

import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

/**
 * @author Rob Johnson, Aberystwyth University
 * @email rob.johnson@aber.ac.uk
 * @date 21st May 2012
 * 
 * Logging.java
 * 
 * Logging class that deals with all logging to file.
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
public class Logging {
	
	public static Logger startLogFile(String logLevel, String logPath) {
		
		Logger LOG_FILE = null;
		
		Level logging = Level.OFF;
		
		if (logLevel != null) {
			
			if (logLevel.equals("LEVEL0")) {
				logging = Level.SEVERE;
			}
			else if (logLevel.equals("LEVEL1")) {
				logging = Level.INFO;
			}
			else if (logLevel.equals("LEVEL2")) {
				logging = Level.CONFIG;
			}
			else if (logLevel.equals("LEVEL3")) {
				logging = Level.FINE;
			}
			else if (logLevel.equals("LEVEL4")) {
				logging = Level.ALL;
			}
		}
	
		// start a logger
		LOG_FILE = Logger.getLogger("PAWSLog");
		
		try {

			// This block configure the logger with handler and formatter
			FileHandler fh = new FileHandler(logPath, true);
			LOG_FILE.addHandler(fh);
			LOG_FILE.setLevel(logging);
			SimpleFormatter formatter = new SimpleFormatter();
			fh.setFormatter(formatter);

	    } 
		catch (SecurityException e) {
			e.printStackTrace();
		} 
		catch (IOException e) {
			e.printStackTrace();
		}
		
		return LOG_FILE;
	}
}
